package com.sinoglobal.ningxia.activity.vip;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.activity.base.BaseActivity;
import com.sinoglobal.ningxia.beans.RootVo;
import com.sinoglobal.ningxia.downdata.imp.RemoteImpl;
import com.sinoglobal.ningxia.http.Code;
import com.sinoglobal.ningxia.utils.ItktLog;
import com.sinoglobal.ningxia.utils.MContants;
import com.sinoglobal.ningxia.utils.ValidUtil;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @author lms
 * @title: InputCodeActivity.java
 * @package com.sinoglobal.ningxia.activity.vip
 * @description:输入验证码页面
 * @date 2014-6-25 下午4:24:08
 */
public class CodeInput extends BaseActivity implements OnClickListener {
    // 下一步,异常信息
    private TextView next, error;
    // 输入验证码
    private EditText inputCode;
    // 获取验证码
    private TextView getCode;
    // 手机号码字符串
    private String phoneNum;
    // 验证码字符串
    private String codeStr;
    private Intent intent;
    // 计时时间
    private int time = 60;
    private Timer timer;
    // 传递的activity名称
    private String activityName;
    // 用于判断注册后跳转页面的字符串
    private String registActivityName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_vip_input_code);
        openChenJInShi();//开启沉浸式
        init();
        getTime();
        setListener();
    }

    // 得到找回密码的验证码
    private void getFindPwdCode() {
        new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.sending), true, true) {

            @Override
            public RootVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().getFindPwdCode(phoneNum);
            }

            @Override
            public void after(RootVo result) {
                if (null == result) { // 数据为空，显示reLoad
                    showReLoading();
                    return;
                }

                if (Code.CODE_SUCCESS != result.getCode()) {
                    showShortToastMessage(result.getMessage());
                    return;
                }
                getTime();
            }

            @Override
            public void exception() {

            }
        }.execute();
    }

    // 得到用户注册的短信验证码
    private void getUserRegistCode() {

        new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.sending), true, true) {

            @Override
            public RootVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().getRegistUserCode(phoneNum);
            }

            @Override
            public void after(RootVo result) {
                // 数据为空，显示reLoad
                if (null == result) {
                    showReLoading();
                    return;
                }

                if (Code.CODE_SUCCESS != result.getCode()) {
                    showShortToastMessage(result.getMessage());
                    return;
                }
                getTime();
            }

            @Override
            public void exception() {

            }
        }.execute();

    }

    // 获得倒计时时间
    public void getTime() {
        getCode.setClickable(false);
        timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        time--;
                        getCode.setText(time + getResources().getString(R.string.get_code_time));
                        getCode.setBackgroundResource(R.drawable.bg_code);
                        if (time < 0) {
                            timer.cancel();
                            time = 60;
                            getCode.setClickable(true);
                            getCode.setBackgroundResource(R.drawable.bg_forget_pwd);
                            getCode.setText(getResources().getString(R.string.get_code));

                        }
                    }
                });
            }
        };
        timer.schedule(task, 1000, 1000);
    }

    private void setListener() {
        next.setOnClickListener(this);
        getCode.setOnClickListener(this);
    }

    private void init() {
        titleView.setText(R.string.input_code_title);
        next = (TextView) findViewById(R.id.next_tv);
        inputCode = (EditText) findViewById(R.id.edit_input_code);
        getCode = (TextView) findViewById(R.id.get_code_tv);
        error = (TextView) findViewById(R.id.code_error_tv);
        phoneNum = getIntent().getStringExtra("phoneNum");
        activityName = getIntent().getStringExtra("activity");
        registActivityName = getIntent().getStringExtra("activityName");
        error.setText(getResources().getString(R.string.send_code_to_phone) + phoneNum);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.get_code_tv:
                inputCode.setText(MContants.EMPTY_STRING);
                // 根据传递的activity的名称，发送不同的验证码
                if ("regist".equals(activityName)) {
                    getUserRegistCode();
                } else if ("reset_pwd".equals(activityName)) {
                    getFindPwdCode();
                }
                break;

            case R.id.next_tv:
                // 如果从注册页面跳转过来的就验证注册的验证码，如果从找回密码页面跳转就验证找回密码的验证码
                if ("regist".equals(activityName)) {
                    validCode(2);
                } else if ("reset_pwd".equals(activityName)) {
                    validCode(1);
                }
                break;
        }
    }

    // 对验证码进行验证,type=1时为找回密码验证，type=2是注册验证
    private void validCode(final int type) {
        codeStr = inputCode.getText().toString().trim();
        ItktLog.i("code" + codeStr);
        String message = ValidUtil.validCheckCode(codeStr);
        if (!MContants.EMPTY_STRING.equals(message)) {
            showShortToastMessage(message);
            return;
        }
        new ItktAsyncTaskWithDialog<Void, Void, RootVo>(getResources().getString(R.string.validating), true, true) {

            @Override
            public RootVo before(Void... params) throws Exception {
                return RemoteImpl.getInstance().validCode(phoneNum, type, codeStr);
            }

            @Override
            public void after(RootVo result) {
                // 数据为空
                if (null == result) {
                    return;
                }

                if (Code.CODE_SUCCESS != result.getCode()) {
                    showShortToastMessage(result.getMessage());
                    return;
                }
                // type=1时跳转到重置密码页面，type=2时跳转到输入注册信息页面
                if (1 == type) {
                    intent = new Intent(CodeInput.this, PasswordReset.class);
                } else if (2 == type) {
                    intent = new Intent(CodeInput.this, RegistInfoSet.class);
                }
                intent.putExtra("username", phoneNum);
                intent.putExtra("activityName", registActivityName);
                startActivity(intent);
                pushActivity(CodeInput.this);
            }

            @Override
            public void exception() {

            }
        }.execute();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 释放timer，结束timer中的thread
        timer.cancel();
        finish();
    }
}
